#include "ViewEventFilter.h"

#include <QApplication>
#include <QInputContext>
#include <QDebug>

ViewEventFilter::ViewEventFilter(QObject *parent)
    : QObject(parent)
    , m_prevFocusWidget(0)
{
}

bool ViewEventFilter::eventFilter(QObject *obj, QEvent *event)
{
    QInputContext *ic = qApp->inputContext();
    if (ic) {
        //qDebug() << "ViewEventFilter::eventFilter current=" << ic->focusWidget() << "prev=" << m_prevFocusWidget;
        if (!ic->focusWidget() && m_prevFocusWidget) {
            qDebug() << "ViewEventFilter::eventFilter CLOSE current=" << ic->focusWidget() << "prev=" << m_prevFocusWidget;
            QEvent closeSIPEvent(QEvent::CloseSoftwareInputPanel);
            ic->filterEvent(&closeSIPEvent);
        } else if (!m_prevFocusWidget && ic->focusWidget()) {
            qDebug() << "ViewEventFilter::eventFilter OPEN current=" << ic->focusWidget() << "prev=" << m_prevFocusWidget;
            QEvent openSIPEvent(QEvent::RequestSoftwareInputPanel);
            ic->filterEvent(&openSIPEvent);
        } else {
            //qDebug() << "ViewEventFilter::eventFilter NOTHING current=" << ic->focusWidget() << "prev=" << m_prevFocusWidget;
        }
        m_prevFocusWidget = ic->focusWidget();
    }
    return QObject::eventFilter(obj,event);
}
